package com.yejhxt.api;

import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.google.gson.Gson;
import com.yejhxt.apiInterface.UserInterface;
import com.yejhxt.controllerImpl.UserController;
import com.yejhxt.inter.roleMapper;
import com.yejhxt.inter.userMapper;
import com.yejhxt.tools.ProjectTools;

@Controller
@RequestMapping(value = "api/user", method = RequestMethod.POST)
public class User implements UserInterface {

	@Resource
	private userMapper userMapper;
	@Resource
	private roleMapper roleMapper;

	boolean first;

	void prepare(HttpServletResponse response) {
		if (first) {
			uc = new UserController(userMapper, roleMapper);
			first = false;
		}
		response.setContentType("application/json;charset=UTF-8");
		response.setCharacterEncoding("UTF-8");
	}

	UserController uc;

	public User() {
		first = true;
	}

	@RequestMapping("getValidateCode.do")
	public void getMessage(@RequestParam(value = "phonenumber", defaultValue = "") String PhoneNumber,
			@RequestParam(value = "isforgot", defaultValue = "") String isforgot,
			@RequestParam(value = "web", defaultValue = "") String web, HttpServletRequest request, Model model,
			HttpSession session, HttpServletResponse response) {
		prepare(response);
		int status = uc.getMessage(PhoneNumber, session,isforgot);

		Map<String, Object> map = ProjectTools.returnMap(status);
		if (status == 200)
			map.put("validatecodeid", session.getId());

		String json = new Gson().toJson(map);
		ProjectTools.ResponseOut(response, json);
	}

	@RequestMapping("register.do")
	public void registerHandler(@RequestParam(value = "phonenumber", defaultValue = "") String PhoneNumber,
			@RequestParam(value = "validatecodeid", defaultValue = "") String vaidatecodeid,
			@RequestParam(value = "password", defaultValue = "") String Password,
			@RequestParam(value = "validatecode", defaultValue = "") String validateCode,
			@RequestParam(value = "web", defaultValue = "") String web, HttpServletRequest request,
			HttpServletResponse response, Model model, HttpSession session) {
		prepare(response);
		if (StringUtils.isEmpty(web))// from api
			session = ProjectTools.getAppSession(vaidatecodeid);

		int status = uc.register(PhoneNumber, Password, validateCode, session, web);

		Map<String, Object> map = ProjectTools.returnMap(status);
		if (status == 200) {
			map.put("access_token", session.getId());
			map.put("expires_in", session.getMaxInactiveInterval());
		}

		String json = new Gson().toJson(map);
		ProjectTools.ResponseOut(response, json);
	}

	@RequestMapping("authorize.do")
	public void LoginHandler(@RequestParam(value = "phonenumber", defaultValue = "") String PhoneNumber,
			@RequestParam(value = "password", defaultValue = "") String Password,
			@RequestParam(value = "web", defaultValue = "") String web, HttpServletRequest request,
			HttpServletResponse response, Model model, HttpSession session) {
		prepare(response);
		int status = uc.LoginHandler(PhoneNumber, Password, session, web);

		Map<String, Object> map = ProjectTools.returnMap(status);
		if (status == 200) {
			map.put("access_token", session.getId());
			map.put("expires_in", session.getMaxInactiveInterval());
		}
		String json = new Gson().toJson(map);
		ProjectTools.ResponseOut(response, json);
	}

	@RequestMapping("passwordUpdate.do")
	@Override
	public void PasswordUpdateHandler(@RequestParam(value = "access_token", defaultValue = "") String sid,
			@RequestParam(value = "password", defaultValue = "") String Password,
			@RequestParam(value = "newpassword", defaultValue = "") String newPassword,
			@RequestParam(value = "web", defaultValue = "") String web, HttpServletRequest request,
			HttpServletResponse response, Model model, HttpSession session) {
		prepare(response);
		if (StringUtils.isEmpty(web))// from api
			session = ProjectTools.getAppSession(sid);
		int status = uc.PasswordUpdateHandler( Password, newPassword, session, web);
		
		Map<String, Object> map = ProjectTools.returnMap(status);
		if (status == 200) {
			map.put("access_token", session.getId());
			map.put("expires_in", session.getMaxInactiveInterval());
		}
		String json = new Gson().toJson(map);
		ProjectTools.ResponseOut(response, json);
	}
	
	@RequestMapping("passwordForgot.do")
	@Override
	public void PasswordForgotHandler(@RequestParam(value = "phonenumber", defaultValue = "") String PhoneNumber,
			@RequestParam(value = "validatecodeid", defaultValue = "") String vaidatecodeid,
			@RequestParam(value = "password", defaultValue = "") String Password,
			@RequestParam(value = "validatecode", defaultValue = "") String validateCode,
			@RequestParam(value = "web", defaultValue = "") String web, HttpServletRequest request,
			HttpServletResponse response, Model model, HttpSession session) {
		prepare(response);
		if (StringUtils.isEmpty(web))// from api
			session = ProjectTools.getAppSession(vaidatecodeid);

		int status = uc.PasswordForgotHandler(PhoneNumber, Password, validateCode, session, web);

		Map<String, Object> map = ProjectTools.returnMap(status);
		if (status == 200) {
			map.put("access_token", session.getId());
			map.put("expires_in", session.getMaxInactiveInterval());
		}

		String json = new Gson().toJson(map);
		ProjectTools.ResponseOut(response, json);
	}
}